package com.kangfu.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.kangfu.api.service.ILoginService;
import com.kangfu.api.service.ITokenService;
import com.kangfu.api.service.IUserService;
import com.kangfu.common.constant.WechatConstant;
import com.kangfu.common.exception.BusinessException;
import com.kangfu.db.domain.entity.User;
import com.kangfu.db.mapper.LessonMapper;

import com.kangfu.wechat.api.LoginApi;
import com.kangfu.wechat.bean.result.OpenidRes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    public IUserService userService;

    @Autowired
    public ITokenService tokenService;


    @Value("${spring.datasource.druid.master.password}")
    private String mysqlPwd;

    private static final Logger log = LoggerFactory.getLogger(LoginServiceImpl.class);

    public HashMap<String, Object> login(String code) {
        /** ======================== 测试start========================== */
        if ("123456".equals(mysqlPwd)) {
            HashMap<String, Object> hashMap = new HashMap<String, Object>();
            String token = tokenService.genrateToken(30, "ofm8k4xNIPQGp1_0lZVTwyCcKyhU");
            hashMap.put("token", token);
            hashMap.put("userInfo",false);
            hashMap.put("phone",false);
            return hashMap;
        }

        /** ========================end========================== */

        OpenidRes openidRes = LoginApi.getOpenID(WechatConstant.APPID, WechatConstant.SECRET, code);

        if (openidRes != null && 0 == openidRes.getErrcode() ) {
            //成功获取到openid
            //检查用户是否存在，不存在就新增用户
            User user = userService.selectUserByOpenid(openidRes.getOpenid());
            //如果用户不存在就新增
            if (user == null) {
                user = new User();
                user.setOpenid(openidRes.getOpenid());
                user.setCreateTime(LocalDateTime.now());
                user.setVersion(2);
                user.setHuaweiStatus("1");
                user.setAvatar("https://qny.bravo.love/avatar/user.png"); //默认头像
                userService.insertUser(user);

            } else if ("1".equals(user.getStatus())) {
                //检查用户状态，如果用户存在并且被禁用了
                throw new BusinessException("用户被禁用");
            }
            //返回用户是否已经获取到手机号和头像，昵称等信息，以便于前端判断是否需要获取用户手机号，头像，昵称
            HashMap<String, Object> hashMap = new HashMap<String, Object>();
            //颁发token
            String token = tokenService.genrateToken(user.getId(), user.getOpenid());
            hashMap.put("token", token);
            if (StrUtil.isBlank(user.getPhone())) {
                hashMap.put("phone",false);
            } else {
                hashMap.put("phone",true);
            }
            if (StrUtil.isBlank(user.getAvatar()) && StrUtil.isBlank(user.getNickname())) {
                hashMap.put("userInfo",false);
            } else {
                hashMap.put("userInfo",true);
            }
            return hashMap;
        } else {
            throw new BusinessException("用户登录失败");
        }
    }


}
